package com.itheima.wechat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.JedisUtl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;


    @Autowired
    JedisUtl jedisUtil;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(String token){
        //实战7、移动端页面使用redis缓存套餐数据
        String setMealList = jedisUtil.get("setmeals");
        if (setMealList != null) {
            //把从redis中查到的数据转换成List<Setmeal> 返回到前端
            return Result.OK("", JSON.parseArray(setMealList,Setmeal.class) );
        }
       List<Setmeal> setmeals =  setmealService.getSetmeal();
        jedisUtil.setex("setmeals",60*60*2, JSON.toJSONString(setmeals));
       return Result.OK("",setmeals);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);
        return Result.OK("",setmeal);
    }

}
